package com.xiaoyu.studyworkspace.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.xiaoyu.studyworkspace.StudyworkspaceApplication;
import com.xiaoyu.studyworkspace.common.kapaentity.register.apperbean.*;
import com.xiaoyu.studyworkspace.common.kapaentity.scan.PhoenixResp;
import com.xiaoyu.studyworkspace.common.kapaentity.scan.PhoenixUtil;
import com.xiaoyu.studyworkspace.common.kapaentity.scan.KapaReset;
import com.xiaoyu.studyworkspace.common.kapaentity.scan.TenXun;
import com.xiaoyu.studyworkspace.common.kapaentity.websocket.KapaCode;
import com.xiaoyu.studyworkspace.common.kapaentity.userinfo.RootBean;
import com.xiaoyu.studyworkspace.common.kapaentity.register.appbooting.AppBooting;
import com.xiaoyu.studyworkspace.common.kapaentity.register.authbean.AuthBean;
import com.xiaoyu.studyworkspace.common.kapaentity.register.invitebean.InviteBean;
import com.xiaoyu.studyworkspace.common.kapaentity.register.login.UserLogin;
import com.xiaoyu.studyworkspace.common.kapaentity.register.mobilecheck.MobileCheckPhone;
import com.xiaoyu.studyworkspace.common.kapaentity.register.personinfo.PersonInfo;
import com.xiaoyu.studyworkspace.common.kapaentity.register.registerbean.RegisterBean;
import com.xiaoyu.studyworkspace.common.kapaentity.register.transferbean.Transfer;
import com.xiaoyu.studyworkspace.common.kapaentity.register.transferbean.TransferBean;
import com.xiaoyu.studyworkspace.common.kapaentity.register.zhuhao.ZhuHaoResponse;
import com.xiaoyu.studyworkspace.common.kapaentity.register.selectinfo.SelectInfo;
import com.xiaoyu.studyworkspace.common.util.EMaURL;
import com.xiaoyu.studyworkspace.common.util.KapaHttpUtil;
import com.xiaoyu.studyworkspace.common.util.KapaParam;
import com.xiaoyu.studyworkspace.dto.EMaCheckDTO;
import com.xiaoyu.studyworkspace.dto.EMaRegisterDTO;
import com.xiaoyu.studyworkspace.dto.EMaUserInfoDTO;
import com.xiaoyu.studyworkspace.dto.TransferDTO;
import com.xiaoyu.studyworkspace.mapper.AutoRegisterMapper;
import com.xiaoyu.studyworkspace.pojo.*;
import com.xiaoyu.studyworkspace.properties.UserCheckKapaProperties;
import com.xiaoyu.studyworkspace.service.AutoRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class AutoRegisterServiceImpl implements AutoRegisterService {

    @Autowired
    private AutoRegisterMapper autoRegisterMapper;


    @Autowired
    private UserCheckKapaProperties userCheckKapaProperties;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    @Async
    public void scanPhone2(String realPath) throws InterruptedException, IOException {

        String userLogin = HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/login?account=190190&password=zhenfu007&loginsource=API");
        PhoenixResp phoenixResp = JSONUtil.toBean(userLogin, PhoenixResp.class);
        if (phoenixResp.getCode() != 1) {
            //错误
            log.info("登录错误：{}", userLogin);
            return;
        }
        String authToken = phoenixResp.getData().getAuthtoken();
        String balance = HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/user/loadUserWallet?Authorization=" + authToken);
        log.info("接码平台余额:{}", balance);
        int sum = 0;
        while (true) {
            sum++;
            if (sum > 500) {
                log.info("扫号500次");
                return;
            }
            //进行第一个手机号
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip异常");
                throw new RuntimeException("ip异常");
            }
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.DETECTION);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());

            log.info("开始获取号码");
            MobileCheckPhone mobileCheckPhone = new MobileCheckPhone();
            mobileCheckPhone.setSms_type(1);
            //2.获取一个号码
            String getPhone = HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/loadReceivePhone?Authorization=" + authToken + "&itemid=13848&itemtype=0&phonetype=0");
            PhoenixResp getPhoneResp = JSONUtil.toBean(getPhone, PhoenixResp.class);
            log.info(getPhoneResp.getData().getItemlist().getPhone());
            if (phoenixResp.getCode() != 1) {
                //错误
                log.info("获取手机号错误：{}", getPhoneResp);
                continue;
            }
            mobileCheckPhone.setMobile(getPhoneResp.getData().getItemlist().getPhone());
            paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(mobileCheckPhone));
            /**
             * 使用代理
             */
            RegisterBean registerBean = KapaHttpUtil.getRegisterBean(paramMap);
            log.info("注册信息查看：{}", registerBean);
            int code = registerBean.getCode();
            //存在
            if (code == 14) {
                //从数据库这种查询是否已经存在，
                ScanPhone scanPhone = autoRegisterMapper.selectPhoneExits(mobileCheckPhone.getMobile());
                if (scanPhone != null) {
                    log.info("数据库中已存在,{}", mobileCheckPhone.getMobile());
                }
                if (scanPhone == null) {
                    log.info("数据库不存在,{}", mobileCheckPhone.getMobile());
                    TenXun tenXun = KapaHttpUtil.getTxTicket();
                    log.info("调用api获取滑块密钥:{}", tenXun);
                    //检查
                    KapaReset kapaReset = new KapaReset();
                    kapaReset.setMobile(mobileCheckPhone.getMobile());
                    kapaReset.setSms_type(3);
                    kapaReset.setRandstr(tenXun.getResult().getRandstr());
                    kapaReset.setTicket(tenXun.getResult().getTicket());
                    paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(kapaReset));
                    /**
                     * 返回验证码凭证
                     * 使用代理
                     */
                    RootBean rootBean = KapaHttpUtil.getRootBean(paramMap);
                    if (rootBean == null) {
                        return;
                    }
                    log.info("验证码凭证：{}", rootBean);
                    //接码
                    int count = 0;
                    boolean flag = false;
                    String yzm = "";
                    while (true) {
                        //接码
                        String phoneMsg = HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/getPutSmsResult?Authorization=" + authToken + "&comid=" + getPhoneResp.getData().getComid());
                        log.info("接码信息：{}", phoneMsg);
                        PhoenixResp phoenixResp1 = JSONUtil.toBean(phoneMsg, PhoenixResp.class);
                        count++;
                        Thread.sleep(4000);
                        if (count == 4) {
                            log.info("超时");
                            flag = true;
                            break;
                        }
                        //对验证码进行处理
                        log.info("获取验证码处理中");
                        if (StrUtil.isNotEmpty(phoenixResp1.getData().getComdata().getCom_code())) {
                            yzm = phoenixResp1.getData().getComdata().getCom_code();
                            log.info("获取验证码:{}", phoenixResp1.getData().getComdata().getCom_code());
                            break;
                        }
                    }
                    //超时
                    if (flag) {
                        //释放号码
                        HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitBlockPhoneSingle?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile() + "&remark=已经使用过");
                        HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitRelasePhone?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile());
                        continue;
                    }
                    KapaReset kapaReset1 = new KapaReset();
                    kapaReset1.setCode(Integer.parseInt(yzm));
                    String password = "zhenfu007";
                    String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + password).getBytes());
                    kapaReset1.setLogin_password(passwordMd5);
                    kapaReset1.setMobile(mobileCheckPhone.getMobile());
                    kapaReset1.setMobile_check_sn(rootBean.getData().getMobile_check_sn());
                    paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(kapaReset1));
                    paramMap.put(KapaParam.CODE, KapaCode.RESET);
                    /**
                     * 重置成功结果resetResp1
                     */
                    String resetResp1 = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));

                    //重置成功，写入文本,写入数据库
                    ScanPhone scanPhone1 = new ScanPhone();
                    scanPhone1.setPhone(mobileCheckPhone.getMobile());
                    scanPhone1.setCreate_time(LocalDateTime.now());
                    autoRegisterMapper.insertScanPhone(scanPhone1);
                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(realPath + "reset-data.txt", true));
                    bufferedWriter.write(mobileCheckPhone.getMobile() + "----" + password);
                    //插入换行
                    bufferedWriter.newLine();
                    bufferedWriter.close();
                    //拉黑号码
                    HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitBlockPhoneSingle?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile() + "&remark=已经使用过");
                    //释放号码
                    HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitRelasePhone?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile());
                    continue;
                }
            }
            //不存在就丢弃，拉黑号码
            if (code == 0) {
                log.info("手机号未注册：{}", mobileCheckPhone.getMobile());
                //拉黑
                HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitBlockPhoneSingle?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile() + "&remark=已经使用过");
                //释放号码
                HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitRelasePhone?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile());
                continue;
            }
            //释放号码
            HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitRelasePhone?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile());
            //拉黑
            HttpUtil.get(PhoenixUtil.PhoenixURL + "/api/phone/submitBlockPhoneSingle?Authorization=" + authToken + "&itemid=13848&phone=" + mobileCheckPhone.getMobile() + "&remark=已经使用过");

        }


    }


    @Override
    @Async
    public void scanPhone(String realPath) throws InterruptedException, IOException {

        String userLogin = HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=login&user=qiyou1202&password=zhang1202");
        if (userLogin.contains("ERROR")) {
            //错误
            log.info("登录错误：{}", userLogin);
            return;
        }
        String balance = HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=leftAmount&token=" + userLogin);
        log.info("接码平台余额:{}", balance);

        int sum = 0;
        while (true) {
            sum++;
            if (sum > 500) {
                log.info("扫号500次");
                return;
            }
            //进行第一个手机号
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip异常");
                return;
            }
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.DETECTION);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());

            log.info("开始获取号码");
            MobileCheckPhone mobileCheckPhone = new MobileCheckPhone();
            mobileCheckPhone.setSms_type(1);
            //2.获取一个号码
            String getPhone = HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=getPhone&token=" + userLogin + "&phone=&province=&cardType=全部");
            log.info(getPhone);
            if (getPhone.contains("ERROR")) {
                //错误
                log.info("获取手机号错误：{}", getPhone);
                continue;
            }
            mobileCheckPhone.setMobile(getPhone);
            paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(mobileCheckPhone));
            /**
             * 使用代理
             */
            RegisterBean registerBean = KapaHttpUtil.getRegisterBean(paramMap);
            if (registerBean == null) {
                log.error("获取RegisterBean错误,{}", registerBean);
                continue;
            }
            log.info("注册信息查看：{}", registerBean);
            int code = registerBean.getCode();
            //存在
            if (code == 14) {
                //从数据库这种查询是否已经存在，
                ScanPhone scanPhone = autoRegisterMapper.selectPhoneExits(mobileCheckPhone.getMobile());
                if (scanPhone != null) {
                    log.info("数据库中已存在,{}", mobileCheckPhone.getMobile());
                    continue;
                }
                log.info("数据库不存在,{}", mobileCheckPhone.getMobile());
                TenXun tenXun = KapaHttpUtil.getTxTicket();
                if (tenXun == null) {
                    log.error("腾讯云验证码出错：{}", tenXun);
                    return;
                }
                log.info("调用api获取滑块密钥:{}", tenXun);
                //检查
                KapaReset kapaReset = new KapaReset();
                kapaReset.setMobile(mobileCheckPhone.getMobile());
                kapaReset.setSms_type(3);
                kapaReset.setRandstr(tenXun.getResult().getRandstr());
                kapaReset.setTicket(tenXun.getResult().getTicket());
                paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(kapaReset));
                /**
                 * 返回验证码凭证
                 * 使用代理
                 */
                RootBean rootBean = KapaHttpUtil.getRootBean(paramMap);
                if (rootBean == null) {
                    log.error("RootBean错误{},", rootBean);
                    continue;
                }
                log.info("验证码凭证：{}", rootBean);
                //接码
                int count = 0;
                boolean flag = false;
                String yzm = "";
                while (true) {
                    count++;
                    Thread.sleep(10000);
                    String phoneMsg = HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=getMsg&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile() + "&keyWord=卡帕部落");
                    log.info("接码信息：{}", phoneMsg);
                    if (count == 4) {
                        log.info("超时");
                        flag = true;
                        break;
                    }
                    //接码
                    log.info("获取验证码处理中");
                    if (phoneMsg.contains("【卡帕部落】")) {
                        String msg = phoneMsg.substring(phoneMsg.indexOf("验证码：") + 4, phoneMsg.indexOf("验证码：") + 8);
                        log.info("验证码：{}", msg);
                        yzm = msg;
                        break;
                    }
                }
                //超时
                if (flag) {
                    //释放号码
                    HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=release&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
                    HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=block&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
                    continue;
                }
                KapaReset kapaReset1 = new KapaReset();
                kapaReset1.setCode(Integer.parseInt(yzm));
                String password = "zhenfu007";
                String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + password).getBytes());
                kapaReset1.setLogin_password(passwordMd5);
                kapaReset1.setMobile(mobileCheckPhone.getMobile());
                kapaReset1.setMobile_check_sn(rootBean.getData().getMobile_check_sn());
                paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(kapaReset1));
                paramMap.put(KapaParam.CODE, KapaCode.RESET);
                /**
                 * 重置成功结果resetResp1
                 */
                String resetResp1 = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));

                //重置成功，写入文本,写入数据库
                ScanPhone scanPhone1 = new ScanPhone();
                scanPhone1.setPhone(mobileCheckPhone.getMobile());
                scanPhone1.setCreate_time(LocalDateTime.now());
                autoRegisterMapper.insertScanPhone(scanPhone1);
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(realPath + "reset-data.txt", true));
                bufferedWriter.write(mobileCheckPhone.getMobile() + "----" + password);
                //插入换行
                bufferedWriter.newLine();
                bufferedWriter.close();
                //释放号码
                HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=release&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
                //拉黑号码
                HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=block&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
                continue;

            }
            //不存在就丢弃，拉黑号码
            if (code == 0) {
                log.info("手机号未注册：{}", mobileCheckPhone.getMobile());
                //释放号码
                HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=release&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
                //拉黑号码
                HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=block&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
                continue;
            }
            //释放号码
            HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=release&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
            //拉黑号码
            HttpUtil.get(PhoenixUtil.EOMSG_URL + "?code=block&token=" + userLogin + "&phone=" + mobileCheckPhone.getMobile());
        }
    }

    @Override
    public EMaUerInfo loginBySecret2(EMaUserInfoDTO eMaUserInfoDTO) {
        String secret = eMaUserInfoDTO.getSecret();
        EMaUerInfo eMaUerInfo;
        //对接码进行账号分割
        String[] split = secret.split("@");
        ZhuHaoResponse zhuHaoResponse = JSONUtil.toBean(HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=login&user=" + split[0] + "&pass=" + split[1]), ZhuHaoResponse.class);
        if (zhuHaoResponse.getCode() != 0) {
            //ERROR
            return null;
        }
        //查询余额
        ZhuHaoResponse leftAmountBody = JSONUtil.toBean(HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=getSummary&token=" + zhuHaoResponse.getToken()), ZhuHaoResponse.class);
        //登录没问题就保存，从数据库中查询
        eMaUerInfo = autoRegisterMapper.selectEMaUserInfo(split[0]);
        if (eMaUerInfo == null) {
            //插入
            EMaUerInfo eMaUerInfo1 = new EMaUerInfo();
            eMaUerInfo1.setEm_user_id(split[0]);
            eMaUerInfo1.setPassword(split[1]);
            eMaUerInfo1.setToken(zhuHaoResponse.getToken());
            eMaUerInfo1.setPearl(Double.valueOf(leftAmountBody.getMoney()));
            eMaUerInfo1.setCreate_time(LocalDateTime.now());
            autoRegisterMapper.insertEMaUserInfo(eMaUerInfo1);
            eMaUerInfo = eMaUerInfo1;
        } else {
            //果如存在就更新
            eMaUerInfo.setPassword(split[1]);
            eMaUerInfo.setToken(zhuHaoResponse.getToken());
            eMaUerInfo.setPearl(Double.valueOf(leftAmountBody.getMoney()));
            autoRegisterMapper.updateEMaUserInfo(eMaUerInfo);
        }
        return eMaUerInfo;
    }


    @Override
    public EMaUerInfo loginBySecret(EMaUserInfoDTO eMaUserInfoDTO) {
        String secret = eMaUserInfoDTO.getSecret();
        EMaUerInfo eMaUerInfo = new EMaUerInfo();
        //对接码进行账号分割
        String[] split = secret.split("@");
        String loginResp = HttpUtil.get(EMaURL.EM_URL + "?=code=login&user=" + split[0] + "&password=" + split[1]);
        //ERROR
        log.info("登录内容:{}", loginResp);
        if (loginResp.contains("ERROR")) {
            return eMaUerInfo;
        }
        //查询余额
        String leftAmount = HttpUtil.get(EMaURL.EM_URL + "?code=leftAmount&token=" + loginResp);
        //登录没问题就保存，从数据库中查询
        eMaUerInfo = autoRegisterMapper.selectEMaUserInfo(split[0]);
        if (eMaUerInfo == null) {
            //插入
            EMaUerInfo eMaUerInfo1 = new EMaUerInfo();
            eMaUerInfo1.setEm_user_id(split[0]);
            eMaUerInfo1.setPassword(split[1]);
            eMaUerInfo1.setToken(loginResp);
            eMaUerInfo1.setPearl(Double.valueOf(leftAmount));
            eMaUerInfo1.setCreate_time(LocalDateTime.now());
            autoRegisterMapper.insertEMaUserInfo(eMaUerInfo1);
            eMaUerInfo = eMaUerInfo1;
        } else {
            //果如存在就更新
            eMaUerInfo.setPassword(split[1]);
            eMaUerInfo.setToken(loginResp);
            eMaUerInfo.setPearl(Double.valueOf(leftAmount));
            autoRegisterMapper.updateEMaUserInfo(eMaUerInfo);
        }
        return eMaUerInfo;
    }

    @Override
    public String loginReq(EMaCheckDTO eMaCheckDTO) {
        int userId = eMaCheckDTO.getUser_id();
        int password = eMaCheckDTO.getPassword();
        //去数据库中查询
        RegisterUser registerUser = autoRegisterMapper.selectRegisterUserById(userId);
        if (registerUser == null) {
            //说明不存在
            return "用户不存在";
        }
        if (registerUser.getPassword() == password) {
            //密码正确
            return "登录成功";
        }
        return "密码错误";
    }

    /**
     * 可使用线程池进行优化，对任务进行分片处理
     *
     * @param eMaRegisterDTO
     * @param realPath
     * @throws InterruptedException
     * @throws IOException
     */
    @Override
    @Async
    public void register2(EMaRegisterDTO eMaRegisterDTO, String realPath) throws InterruptedException, IOException {

        //开始登录,查询出token
        EMaUerInfo eMaUerInfo = autoRegisterMapper.selectEMaUserInfo(eMaRegisterDTO.getEm_user_id());
        //开始注册
        for (int i = 0; i < eMaRegisterDTO.getSize(); i++) {
            //进行第一个手机号注册
            log.info("进行第--{}--个", i + 1);
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip异常");
                throw new RuntimeException("ip异常");
            }
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.DETECTION);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());
            boolean flag = true;
            while (flag) {
                log.info("开始获取号码");
                String cancelAllRecv = HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=cancelAllRecv&token=" + eMaUerInfo.getToken());
                log.info("释放全部号码{}", cancelAllRecv);
                MobileCheckPhone mobileCheckPhone = new MobileCheckPhone();
                mobileCheckPhone.setSms_type(1);
                //2.获取一个号码API
                ZhuHaoResponse phone1 = JSONUtil.toBean(HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=getPhone&token=" + eMaUerInfo.getToken() + "&sid=75211"), ZhuHaoResponse.class);
                mobileCheckPhone.setMobile(phone1.getPhone());
                paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(mobileCheckPhone));
                String kapaResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                RegisterBean registerBean = JSONUtil.toBean(kapaResponse, RegisterBean.class);
                int code = registerBean.getCode();
                if (code == 14) {
                    //拉黑该手机号，写入数据库
                    //API
                    BlackPhone blackPhone = new BlackPhone();
                    blackPhone.setPhone(mobileCheckPhone.getMobile());
                    blackPhone.setCreate_time(LocalDateTime.now());
                    autoRegisterMapper.insertBlackPhone(blackPhone);
                    log.info("手机号已存在：{}", mobileCheckPhone.getMobile());
                    HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=addBlacklist&token=" + eMaUerInfo.getToken() + "&sid=75211&country_code=&phone=" + mobileCheckPhone.getMobile());
                    //手机号已存在
                    continue;
                }
                if (code == 0) {
                    InviteBean inviteBean = new InviteBean();
                    //成功获取验证
                    //2.携带验证码请求个人信息
                    //收取验证码
                    log.info("该号码可以注册：{}", mobileCheckPhone.getMobile());
                    int count = 0;
                    boolean error = false;
                    while (true) {
                        count++;
                        Thread.sleep(5000);
                        if (count == 4) {
                            error = true;
                            break;
                        }
                        //API获取验证码
                        ZhuHaoResponse phoneMsg = JSONUtil.toBean(HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=getMessage&token=" + eMaUerInfo.getToken() + "&sid=75211&phone=" + mobileCheckPhone.getMobile()), ZhuHaoResponse.class);
                        //对验证码进行处理
                        log.info("获取验证码处理中");
                        if (phoneMsg.getCode() == 0) {
                            log.info("信息：{}", phoneMsg.getSms());
                            log.info("验证码：：{}", phoneMsg.getYzm());
                            inviteBean.setCode(Integer.parseInt(phoneMsg.getYzm()));
                            break;
                        }
                    }
                    if (error) {
                        //拉黑该手机号
                        //API
                        BlackPhone blackPhone = new BlackPhone();
                        blackPhone.setPhone(mobileCheckPhone.getMobile());
                        blackPhone.setCreate_time(LocalDateTime.now());
                        autoRegisterMapper.insertBlackPhone(blackPhone);
                        HttpUtil.get(EMaURL.ZHUHAO_URL + "/sms/?api=addBlacklist&token=" + eMaUerInfo.getToken() + "&sid=75211&country_code=&phone=" + mobileCheckPhone.getMobile());
                        log.info("跳过该号码：{}", mobileCheckPhone.getMobile());
                        continue;
                    }
                    inviteBean.setMobile(mobileCheckPhone.getMobile());
                    inviteBean.setInvite_code(eMaRegisterDTO.getSupperId());
                    inviteBean.setMobile_check_sn(registerBean.getData().getMobile_check_sn());
                    //2.携带验证码请求个人信息，
                    String password = UUID.randomUUID().toString().split("-")[0];
                    String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + password).getBytes(StandardCharsets.UTF_8));
                    inviteBean.setLogin_password(passwordMd5);
                    paramMap.put(KapaParam.CODE, KapaCode.INVITE);
                    paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(inviteBean));
                    String inviteRecord = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                    RootBean rootBean = JSONUtil.toBean(inviteRecord, RootBean.class);
                    if (rootBean.getCode() != 0) {
                        //出现异常，打印
                        log.info("邀请异常：{}", inviteRecord);
                        continue;
                    }
                    if (rootBean.getData().getUser_info().getIdentify_auth_state() == 1) {
                        //进行登录操作，第二步登录
                        paramMap.put(KapaParam.CODE, KapaCode.LOGIN);
                        UserLogin userLogin = new UserLogin();
                        userLogin.setMobile(mobileCheckPhone.getMobile());
                        userLogin.setLogin_password(passwordMd5);
                        paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(userLogin)));
                        String userLoginResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
//                        log.info("第二步登录:" + userLoginResponse);
                        //对登录成功与否进行校验
                        RootBean rootBean2 = JSONUtil.toBean(userLoginResponse, RootBean.class);
                        if (rootBean2.getCode() != 0) {
                            //不为0出现异常
                            if (rootBean2.getCode() != 16) {
                                log.info("登录异常：{}", userLoginResponse);
                                continue;
                            }
                        }
                        //第三步绑定设备信息
                        paramMap.put(KapaParam.DATA, KapaCode.DEVICE_INFO);
                        paramMap.put(KapaParam.CODE, KapaCode.BIND);
                        String bind = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                        //未实名，去实名
                        PersonInfo personInfo = new PersonInfo();
                        int index = 0;
                        while (true) {
                            index++;
                            if (index > 100) {
                                //超时次数
                                break;
                            }
                            //取出一个省份证,3.实名个人信息
                            IdentityCard identityCard = autoRegisterMapper.selectOneIdentityCard();
                            personInfo.setReal_name(identityCard.getReal_name());
                            personInfo.setIdcard(identityCard.getIdcard());
                            String encode = URLEncoder.encode(JSONUtil.toJsonStr(personInfo));
                            paramMap.put(KapaParam.DATA, encode);
                            paramMap.put(KapaParam.CODE, KapaCode.AUTH);
                            autoRegisterMapper.deleteIdentityCardById(identityCard.getId());
                            //认证等待几秒,删除一个
                            Thread.sleep(3000);
                            AuthBean authBean = JSONUtil.toBean(HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap)), AuthBean.class);
                            if (authBean.getCode() == 11) {
                                //身份证号码有误,换一个实名重试
                                log.info("身份证号码有误,正在更换");
                                continue;
                            }
                            if (authBean.getCode() == 13) {
                                String text = authBean.getText();
                                if (text.contains("一个身份证最多实名")) {
                                    log.info("身份证已经使用过,正在更换");
                                    continue;
                                }
                                if (text.contains("已通过")) {
                                    //查询一次，获取名字和身份证
                                    paramMap.put(KapaParam.CODE, KapaCode.GET_INFO);
                                    paramMap.put(KapaParam.DATA, "%7B%7D");
                                    String authResp = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                                    log.info(authResp);
                                    RootBean rootBean1 = JSONUtil.toBean(authResp, RootBean.class);
                                    //已实名
                                    //实名成功,写入数据库当中
                                    AutoRegister autoRegister = new AutoRegister();
                                    BeanUtils.copyProperties(rootBean1.getData().getUser_info(), autoRegister);
                                    autoRegister.setInit_id(appBooting.getInit_id());
                                    autoRegister.setSession_id(appBooting.getSession_id());
                                    autoRegister.setSuperior_id(eMaRegisterDTO.getUser_id());
                                    autoRegister.setIs_transfer(0);
                                    autoRegister.setIs_appear(0);
                                    autoRegister.setPassword(password);
                                    autoRegister.setCreate_time(LocalDateTime.now());
                                    //写入文本
                                    log.info("文件地址：{}", realPath);
                                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(realPath + "copy-data.txt", true));
                                    bufferedWriter.write(autoRegister.getMobile() + "----" + autoRegister.getPassword() + "----" + autoRegister.getSuperior_id());
                                    //插入换行
                                    bufferedWriter.newLine();
                                    bufferedWriter.close();
                                    autoRegisterMapper.insertAutoRegister(autoRegister);
                                    //跳出while
                                    log.info("实名已通过！");
                                    flag = false;
                                    break;
                                }
                            }

                        }
                    }
                    //最后都写入数据库
                }
            }
        }
        //关闭流
        log.info("注册完成：" + eMaRegisterDTO.getSize());
    }

    /**
     * 可使用线程池进行优化，对任务进行分片处理
     *
     * @param eMaRegisterDTO
     * @param realPath
     * @throws InterruptedException
     * @throws IOException
     */
    @Override
    @Async
    public void register(EMaRegisterDTO eMaRegisterDTO, String realPath) throws InterruptedException, IOException {

        //开始登录,查询出token
        //API
        EMaUerInfo eMaUerInfo = autoRegisterMapper.selectEMaUserInfo(eMaRegisterDTO.getEm_user_id());
        //开始注册
        for (int i = 0; i < eMaRegisterDTO.getSize(); i++) {
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip池异常");
                throw new RuntimeException("ip池异常");
            }
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.DETECTION);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());
            boolean flag = true;
            while (flag) {
                MobileCheckPhone mobileCheckPhone = new MobileCheckPhone();
                mobileCheckPhone.setSms_type(1);
                //2.获取一个号码API
                String emResp = HttpUtil.get(EMaURL.EM_URL + "?code=getPhone&token=" + eMaUerInfo.getToken() + "&province=&cardType=全部");
                mobileCheckPhone.setMobile(emResp);
                paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(mobileCheckPhone));
                String kapaResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                RegisterBean registerBean = JSONUtil.toBean(kapaResponse, RegisterBean.class);
                int code = registerBean.getCode();
                if (code == 14) {
                    //拉黑该手机号，写入数据库
                    //API
                    BlackPhone blackPhone = new BlackPhone();
                    blackPhone.setPhone(mobileCheckPhone.getMobile());
                    blackPhone.setCreate_time(LocalDateTime.now());
                    autoRegisterMapper.insertBlackPhone(blackPhone);
                    log.info("手机号已存在：{}", mobileCheckPhone.getMobile());
                    HttpUtil.get(EMaURL.EM_URL + "?code=block&token=" + eMaUerInfo.getToken() + "&phone=" + mobileCheckPhone.getMobile());
                    //手机号已存在
                    continue;
                }
                if (code == 0) {
                    InviteBean inviteBean = new InviteBean();
                    //成功获取验证
                    //2.携带验证码请求个人信息
                    //收取验证码
                    log.info("该号码可以注册：{}", mobileCheckPhone.getMobile());
                    int count = 0;
                    boolean error = false;
                    while (true) {
                        count++;
                        Thread.sleep(5000);
                        if (count == 4) {
                            error = true;
                            break;
                        }
                        //API
                        String phoneResp = HttpUtil.get(EMaURL.EM_URL + "?code=getMsg&token=" + eMaUerInfo.getToken() + "&phone=" + mobileCheckPhone.getMobile() + "&keyWord=卡帕部落");
                        //对验证码进行处理
                        log.info("获取验证码处理中");
                        if (phoneResp.contains("【卡帕部落】")) {
                            String msg = phoneResp.substring(phoneResp.indexOf("验证码：") + 4, phoneResp.indexOf("验证码：") + 8);
                            log.info("验证码：{}", msg);
                            inviteBean.setCode(Integer.valueOf(msg));
                            break;
                        }
                    }
                    if (error) {
                        //拉黑该手机号
                        //API
                        BlackPhone blackPhone = new BlackPhone();
                        blackPhone.setPhone(mobileCheckPhone.getMobile());
                        blackPhone.setCreate_time(LocalDateTime.now());
                        autoRegisterMapper.insertBlackPhone(blackPhone);
                        HttpUtil.get(EMaURL.EM_URL + "?code=block&token=" + eMaUerInfo.getToken() + "&phone=" + mobileCheckPhone.getMobile());
                        log.info("跳过该号码：{}", mobileCheckPhone.getMobile());
                        continue;
                    }
                    inviteBean.setMobile(mobileCheckPhone.getMobile());
                    inviteBean.setInvite_code(eMaRegisterDTO.getSupperId());
                    inviteBean.setMobile_check_sn(registerBean.getData().getMobile_check_sn());
                    //2.携带验证码请求个人信息，
                    String password = UUID.randomUUID().toString().split("-")[0];
                    String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + password).getBytes(StandardCharsets.UTF_8));
                    inviteBean.setLogin_password(passwordMd5);
                    paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(inviteBean));
                    paramMap.put(KapaParam.CODE, KapaCode.INVITE);
                    String inviteRecord = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                    RootBean rootBean = JSONUtil.toBean(inviteRecord, RootBean.class);
                    if (rootBean.getCode() != 0) {
                        //出现异常，打印
                        log.info("邀请异常：{}", inviteRecord);
                        continue;
                    }
                    if (rootBean.getData().getUser_info().getIdentify_auth_state() == 1) {
                        //进行登录操作，第二步登录
                        paramMap.put(KapaParam.CODE, KapaCode.LOGIN);
                        UserLogin userLogin = new UserLogin();
                        userLogin.setMobile(mobileCheckPhone.getMobile());
                        userLogin.setLogin_password(passwordMd5);
                        paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(userLogin)));
                        String userLoginResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                        //对登录成功与否进行校验
                        RootBean rootBean2 = JSONUtil.toBean(userLoginResponse, RootBean.class);
                        if (rootBean2.getCode() != 0) {
                            //不为0出现异常
                            if (rootBean2.getCode() != 16) {
                                log.info("登录异常：{}", userLoginResponse);
                                continue;
                            }
                        }
                        //第三步绑定设备信息
                        paramMap.put(KapaParam.DATA, KapaCode.DEVICE_INFO);
                        paramMap.put(KapaParam.CODE, KapaCode.BIND);
                        HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                        //未实名，去实名
                        PersonInfo personInfo = new PersonInfo();
                        int index = 0;
                        while (true) {
                            index++;
                            if (index > 100) {
                                //超时次数
                                break;
                            }
                            //取出一个省份证,3.实名个人信息
                            IdentityCard identityCard = autoRegisterMapper.selectOneIdentityCard();
                            personInfo.setReal_name(identityCard.getReal_name());
                            personInfo.setIdcard(identityCard.getIdcard());
                            String encode = URLEncoder.encode(JSONUtil.toJsonStr(personInfo));
                            paramMap.put(KapaParam.DATA, encode);
                            paramMap.put(KapaParam.CODE, KapaCode.AUTH);
                            autoRegisterMapper.deleteIdentityCardById(identityCard.getId());
                            //认证等待几秒,删除一个
                            Thread.sleep(3000);
                            log.info("等待实名中");
                            String authResp = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                            AuthBean authBean = JSONUtil.toBean(authResp, AuthBean.class);
                            if (authBean.getCode() == 11) {
                                //身份证号码有误,换一个实名重试
                                log.info("身份证号码有误,正在更换");
                                continue;
                            }
                            if (authBean.getCode() == 13) {
                                String text = authBean.getText();
                                if (text.contains("一个身份证最多实名")) {
                                    log.info("身份证已经使用过,正在更换");
                                    continue;
                                }
                                if (text.contains("已通过")) {
                                    //查询一次，获取名字和身份证
                                    paramMap.put(KapaParam.DATA, "%7B%7D");
                                    paramMap.put(KapaParam.CODE, KapaCode.GET_INFO);
                                    String authResp1 = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                                    log.info(authResp1);
                                    RootBean rootBean1 = JSONUtil.toBean(authResp1, RootBean.class);
                                    //已实名
                                    //实名成功,写入数据库当中
                                    AutoRegister autoRegister = new AutoRegister();
                                    BeanUtils.copyProperties(rootBean1.getData().getUser_info(), autoRegister);
                                    autoRegister.setInit_id(appBooting.getInit_id());
                                    autoRegister.setSession_id(appBooting.getSession_id());
                                    autoRegister.setSuperior_id(eMaRegisterDTO.getUser_id());
                                    autoRegister.setPassword(password);
                                    autoRegister.setIs_transfer(0);
                                    autoRegister.setIs_appear(0);
                                    autoRegister.setCreate_time(LocalDateTime.now());
                                    //写入文本
                                    log.info(realPath);
                                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(realPath + "/copy-data.txt", true));
                                    bufferedWriter.write(autoRegister.getMobile() + "----" + autoRegister.getPassword());
                                    //插入换行
                                    bufferedWriter.newLine();
                                    autoRegisterMapper.insertAutoRegister(autoRegister);
                                    //跳出while
                                    log.info("实名已通过！");
                                    flag = false;
                                    break;
                                }
                            }

                        }
                    }
                    //最后都写入数据库
                }
            }
        }
        log.info("注册完成：" + eMaRegisterDTO.getSize());
    }

    @Override
    public void importIdentityCard() throws IOException {
        InputStream in = StudyworkspaceApplication.class.getClassLoader().getResourceAsStream("6W张身份证.txt");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
        String line;
        int count = 1;
        while ((line = bufferedReader.readLine()) != null) {
            log.info(String.valueOf(count));
            String[] split = line.split("----");
            IdentityCard identityCard = new IdentityCard();
            identityCard.setReal_name(split[0]);
            identityCard.setIdcard(split[1]);
            identityCard.setCreate_time(LocalDateTime.now());
            autoRegisterMapper.insertIdentityCard(identityCard);
            count++;
        }
    }

    @Override
    public List<AutoRegister> getRegisterRecord(Integer userId) {
        List<AutoRegister> autoRegisters = autoRegisterMapper.selectRegisterBysSupId(userId);
        return autoRegisters;
    }

    @Override
    @Async
    public void importAccout(String realPath, String currentId) throws IOException, InterruptedException {
        //取出当前id
        if (currentId == null || "".equals(currentId)) {
            return;
        }
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(realPath)));
        /**
         * 后期对任务分片
         */
        String line;
        int count = 0;
        while ((line = bufferedReader.readLine()) != null) {
            count++;
            log.info(String.valueOf(count));
            String[] split = line.split("----");
            if (split.length != 2) {
                log.info("格式有误：{}", line);
                continue;
            }
            //判断号码是存在
            AutoRegister autoRegister1 = new AutoRegister();
            autoRegister1.setMobile(split[0]);
            autoRegister1.setSuperior_id(Integer.parseInt(currentId));
            List<AutoRegister> isHaveRegisters = autoRegisterMapper.selectIsHaveRegister(autoRegister1);
            if (isHaveRegisters.size() != 0) {
                log.info("不获取appBooting当前：{},存在,不导入", split[0]);
                continue;
            }
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip池异常");
                throw new RuntimeException("ip池异常");
            }
            //第二步，登录
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.LOGIN);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());
            UserLogin userLogin = new UserLogin();
            userLogin.setMobile(split[0]);
            String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + split[1]).getBytes(StandardCharsets.UTF_8));
            userLogin.setLogin_password(passwordMd5);
            paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(userLogin)));
            String userLoginResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            //log.info("第二步结束登录:" + userLoginResponse);
            //密码错误就跳过
            RootBean rootBean2 = JSONUtil.toBean(userLoginResponse, RootBean.class);
            if (rootBean2.getCode() != 0) {
                //不为0出现异常
                log.info("{}-->登录异常：{}", split[0], userLoginResponse);
                continue;
            }
            //第三步绑定设备信息
            paramMap.put(KapaParam.DATA, KapaCode.DEVICE_INFO);
            paramMap.put(KapaParam.CODE, KapaCode.BIND);
            String post = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            //查询个人信息
            paramMap.put(KapaParam.CODE, KapaCode.GET_INFO);
            paramMap.put(KapaParam.DATA, "%7B%7D");
            RootBean rootBean = JSONUtil.toBean(HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap)), RootBean.class);
            if (rootBean.getData().getUser_info().getIdentify_auth_state() == 1) {
                //未实名，去实名，打上标记
                boolean isAuthing = true;
                log.info("正在实名");
                PersonInfo personInfo = new PersonInfo();
                int index1 = 0;
                while (true) {
                    index1++;
                    if (index1 > 50) {
                        //超时次数
                        break;
                    }
                    //取出一个省份证,3.实名个人信息
                    IdentityCard identityCard = autoRegisterMapper.selectOneIdentityCard();
                    personInfo.setReal_name(identityCard.getReal_name());
                    personInfo.setIdcard(identityCard.getIdcard());
                    String encode = URLEncoder.encode(JSONUtil.toJsonStr(personInfo));
                    paramMap.put(KapaParam.CODE, KapaCode.AUTH);
                    paramMap.put(KapaParam.DATA, encode);
                    autoRegisterMapper.deleteIdentityCardById(identityCard.getId());
                    String autoRegisterResp = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                    AuthBean authBean = JSONUtil.toBean(autoRegisterResp, AuthBean.class);
                    //认证等待几秒,删除一个
                    Thread.sleep(3000);
                    log.info("等待实名中");
                    if (authBean.getCode() == 11) {
                        //身份证号码有误,换一个实名重试
                        log.info("身份证号码有误,正在更换:{}", autoRegisterResp);
                        continue;
                    }
                    if (authBean.getCode() == 13) {
                        String text = authBean.getText();
                        if (text.contains("一个身份证最多实名")) {
                            log.info("身份证已经使用过,正在更换:{}", autoRegisterResp);
                            continue;
                        }
                        if (text.contains("已通过")) {
                            //查询一次，获取名字和身份证
                            paramMap.put(KapaParam.CODE, KapaCode.GET_INFO);
                            paramMap.put(KapaParam.DATA, "%7B%7D");
                            String authResp = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                            log.info(authResp);
                            RootBean rootBean1 = JSONUtil.toBean(authResp, RootBean.class);
                            //已实名
                            //实名成功,写入数据库当中
                            AutoRegister autoRegister = new AutoRegister();
                            BeanUtils.copyProperties(rootBean1.getData().getUser_info(), autoRegister);
                            autoRegister.setInit_id(appBooting.getInit_id());
                            autoRegister.setSession_id(appBooting.getSession_id());
                            autoRegister.setSuperior_id(Integer.parseInt(currentId));
                            autoRegister.setIs_transfer(0);
                            autoRegister.setIs_appear(0);
                            autoRegister.setPassword(split[1]);
                            autoRegister.setCreate_time(LocalDateTime.now());
                            autoRegisterMapper.insertAutoRegister(autoRegister);
                            //跳出while
                            log.info("实名已通过！");
                            isAuthing = false;
                            break;
                        }
                    }

                }
                //打上标记,实名的时候已经写入数据库
                if (!isAuthing) {
                    continue;
                }
            }
            //已实名
            //实名成功,写入数据库当中
            AutoRegister autoRegister = new AutoRegister();
            BeanUtils.copyProperties(rootBean.getData().getUser_info(), autoRegister);
            autoRegister.setInit_id(appBooting.getInit_id());
            autoRegister.setSession_id(appBooting.getSession_id());
            autoRegister.setSuperior_id(Integer.parseInt(currentId));
            autoRegister.setIs_transfer(0);
            autoRegister.setIs_appear(0);
            autoRegister.setPassword(split[1]);
            autoRegister.setCreate_time(LocalDateTime.now());
            autoRegisterMapper.insertAutoRegister(autoRegister);
        }
    }

    @Override
    public SelectInfo getUserInfo(String userId) throws IOException {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put(KapaParam.CODE, KapaCode.TRANSFER_INFO);
        paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
        paramMap.put(KapaParam.INIT_ID, userCheckKapaProperties.getInit_id());
        paramMap.put(KapaParam.SESSION_ID, userCheckKapaProperties.getSession_id());

        Transfer transfer = new Transfer();
        transfer.setTransfer_user_id(Integer.parseInt(userId));
        paramMap.put(KapaParam.DATA, JSONUtil.toJsonStr(transfer));
        String userInfo = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
        SelectInfo selectInfo = JSONUtil.toBean(userInfo, SelectInfo.class);
        return selectInfo;
    }

    /**
     * 批量转账
     *
     * @param transferDTO
     */
    @Override
    @Async
    public void transfer(TransferDTO transferDTO) {
        double paySum = 0;
        //1根据user_id查出所有的账号
        String userId = transferDTO.getUser_id();
        List<AutoRegister> autoRegisters = autoRegisterMapper.selectRegisterBysSupId(Integer.valueOf(userId));
        int index = 0;

        //需要缴费
        for (AutoRegister autoRegister : autoRegisters) {
            index++;
            log.info(String.valueOf(index));
            if (autoRegister.getIs_transfer() == 1) {
                log.info("不获取appBooting当前：{},已经转账！", autoRegister.getUser_id());
                continue;
            }
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip池异常");
                throw new RuntimeException("ip池异常");
            }
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.LOGIN);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());

            UserLogin userLogin = new UserLogin();
            userLogin.setMobile(autoRegister.getMobile());
            String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + autoRegister.getPassword()).getBytes(StandardCharsets.UTF_8));
            userLogin.setLogin_password(passwordMd5);
            paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(userLogin)));
            String userLoginResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            RootBean rootBean1 = JSONUtil.toBean(userLoginResponse, RootBean.class);
            if (rootBean1.getCode() != 0) {
                //密码错误
                //出现异常，打印日志
                log.info("第二步结束登录出现异常，打印日志:{}", userLoginResponse);
                log.info("{}号码{}", autoRegister.getUser_id(), rootBean1.getText());
                continue;
            }
            //第三步绑定设备信息
            paramMap.put(KapaParam.DATA, KapaCode.DEVICE_INFO);
            paramMap.put(KapaParam.CODE, KapaCode.BIND);
            String post = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            //查询个人信息
            paramMap.put(KapaParam.CODE, KapaCode.GET_INFO);
            paramMap.put(KapaParam.DATA, "%7B%7D");
            RootBean rootBean = JSONUtil.toBean(HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap)), RootBean.class);
            //4获取个人信息,根据珍珠数量进行转账
            double pearl = rootBean.getData().getUser_info().getPearl();
            //可以转账的数量
            log.info("{}--珍珠数量：{}", rootBean.getData().getUser_info().getUser_id(), pearl);
            //转账的数量
            int number = (int) (pearl / 1.05);
            paySum = paySum + (number * 1.01 * 0.05);
            if (number == 0) {
                log.info("{}--珍珠数量不足1.05个", rootBean.getData().getUser_info().getUser_id());
                autoRegister.setInit_id(appBooting.getInit_id());
                autoRegister.setSession_id(appBooting.getSession_id());
                autoRegister.setIs_transfer(1);
                autoRegisterMapper.updateAutoRegister(autoRegister);
                continue;
            }
            Transfer transfer = new Transfer();
            transfer.setChange_value(number);
            transfer.setTransfer_user_id(Integer.parseInt(transferDTO.getTransfer_user_id()));
            paramMap.put(KapaParam.CODE, KapaCode.TRANSFER);
            paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(transfer)));

            String transferResp = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            TransferBean transferBean = JSONUtil.toBean(transferResp, TransferBean.class);
            //是否成功都需要更新，实名成功,写入数据库当中
            String pearl1 = transferBean.getData().getUim_user_info().getPearl();
            log.info("转账后剩余珍珠：{}", pearl1);
            //5转账,记录日志，更新token,后期更新日志
            log.info("转账日志：{}-->转账珍珠数-->{}至-->{}", transferBean.getData().getSend_user_id(), transferBean.getData().getChange_value(), transferBean.getData().getTransfer_user_id());
            autoRegister.setPearl(Double.parseDouble(pearl1));
            autoRegister.setInit_id(appBooting.getInit_id());
            autoRegister.setSession_id(appBooting.getSession_id());
            autoRegister.setIs_transfer(1);
            autoRegisterMapper.updateAutoRegister(autoRegister);
        }
        log.info("{},本次转账id为{},应付:{}", LocalDateTime.now(), userId, paySum);
        redisTemplate.opsForValue().set("transfer_" + LocalDateTime.now() + "_" + userId, paySum);
    }

    @Override
    public void startAppear(int userId) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //1根据user_id查出所有的账号
        List<AutoRegister> autoRegisters = autoRegisterMapper.selectRegisterBysSupId(userId);
        int index = 0;
        for (AutoRegister autoRegister : autoRegisters) {
            index++;
            //使用账号密码进行登录
            if (autoRegister.getIs_appear() == 1) {
                log.info("不获取appBooting当前：{}号码已经上阵", autoRegister.getMobile());
                continue;
            }
            AppBooting appBooting = KapaHttpUtil.getAppBooting();
            if (appBooting == null) {
                log.info("ip池异常");
                throw new RuntimeException("ip池异常");
            }
            HashMap<String, Object> paramMap = new HashMap<>();
            paramMap.put(KapaParam.CODE, KapaCode.LOGIN);
            paramMap.put(KapaParam.DEPLOY_APP_ID, KapaCode.DEPLOY_APP_ID);
            paramMap.put(KapaParam.INIT_ID, appBooting.getInit_id());
            paramMap.put(KapaParam.SESSION_ID, appBooting.getSession_id());

            UserLogin userLogin = new UserLogin();
            userLogin.setMobile(autoRegister.getMobile());
            String passwordMd5 = DigestUtils.md5DigestAsHex(("login_password_" + autoRegister.getPassword()).getBytes(StandardCharsets.UTF_8));
            userLogin.setLogin_password(passwordMd5);
            paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(userLogin)));
            String userLoginResponse = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            RootBean rootBean1 = JSONUtil.toBean(userLoginResponse, RootBean.class);
            if (rootBean1.getCode() != 0) {
                //密码错误
                //出现异常，打印日志
                log.info("第二步结束登录出现异常,打印日志:{}", userLoginResponse);
                log.info("{}号码{}", autoRegister.getUser_id(), rootBean1.getText());
                continue;
            }
            //第三步绑定设备信息
            paramMap.put(KapaParam.DATA, KapaCode.DEVICE_INFO);
            paramMap.put(KapaParam.CODE, KapaCode.BIND);
            String post = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            //第四步查看背包信息
            paramMap.put(KapaParam.CODE, KapaCode.KNAPSACK);
            paramMap.put(KapaParam.DATA, "%7B%7D");
            String knapsack = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            AppearBean appearBean = JSONUtil.toBean(knapsack, AppearBean.class);
            Knapsack knapsack1 = appearBean.getData().getKnapsack();
            Field[] fields = knapsack1.getClass().getDeclaredFields();
            //全部上阵
            for (Field field1 : fields) {
                String fieldName = field1.getName();
                //获取getter方法
                Method m = knapsack1.getClass().getMethod("get" + fieldName);
                int fieldValue = (int) m.invoke(knapsack1);
                //不为0则上阵
                if (fieldName.equals("A")) {
                    continue;
                }
                if (fieldValue != 0) {
                    Card card = new Card();
                    card.setType(fieldName);
                    paramMap.put(KapaParam.CODE, KapaCode.APPEAR);
                    paramMap.put(KapaParam.DATA, URLEncoder.encode(JSONUtil.toJsonStr(card)));
                    String appearResp = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
                    CardBean cardBean = JSONUtil.toBean(appearResp, CardBean.class);
                    if (cardBean.getCode() != 0) {
                        //出现异常
                        log.info("号码{}可能有多余的卡-->{}", autoRegister.getMobile(), knapsack1);
                        log.info("序号:{}-->ID:{}-->号码上阵异常:{}", index, rootBean1.getData().getUser_info().getMobile(), appearResp);
                    }
                }

            }
            //查看上阵信息
            paramMap.put(KapaParam.CODE, KapaCode.APPEAR_INFO);
            String appearInfo = HttpUtil.post(KapaCode.URL, KapaHttpUtil.convertParam(paramMap));
            AppearBean appearBean1 = JSONUtil.toBean(appearInfo, AppearBean.class);
            Hero_mountain heroMountain = appearBean1.getData().getHero_mountain();
            Field[] declaredFields = heroMountain.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                String fieldName = declaredField.getName();
                Method method = heroMountain.getClass().getMethod("get" + fieldName);
                int fieldValue = (int) method.invoke(heroMountain);
                if (fieldName.equals("A")) {
                    continue;
                }
                if (fieldValue != 0) {
                    //不为空说说明已经上阵
                    autoRegister.setIs_appear(1);
                    autoRegisterMapper.updateAutoRegister(autoRegister);
                    log.info("序号:{}-->{}号码已经上阵:", index, rootBean1.getData().getUser_info().getMobile());
                }
            }
        }
    }


}
